package com.xc.h05.service.wyl.impl;

import com.xc.h05.entity.Appointment;
import com.xc.h05.entity.Consult;
import com.xc.h05.entity.User;
import com.xc.h05.mapper.AppointmentMapper;
import com.xc.h05.mapper.ConsultMapper;
import com.xc.h05.mapper.UserMapper;
import com.xc.h05.service.wyl.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ConsultMapper consultMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Appointment record) {
        return appointmentMapper.insert(record);
    }

    @Override
    public int insertSelective(Appointment record) {
        return appointmentMapper.insertSelective(record);
    }

    @Override
    public Appointment selectByPrimaryKey(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Appointment record) {
        return appointmentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Appointment record) {
        return appointmentMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Appointment> findAll() {
        return appointmentMapper.findAll();
    }

    @Override
    public List<Map<String, Object>> findConsultInfo() {
        return appointmentMapper.findConsultInfo();
    }

    @Override
    public List<Appointment> findByIds(List list,long userId) {
        List<Appointment> appointmentList = appointmentMapper.findByIds(list);
        List<Appointment> appointmentList1 = new ArrayList<>();
        User user1 = userMapper.findById(userId);

        for (Appointment appointment : appointmentList) {
            if (appointment.getUserId() == user1.getId()){
                User user = userMapper.findById(appointment.getUserId());
                appointment.setUser(user);
                Consult consult = consultMapper.findById(Math.toIntExact(appointment.getConsultId()));
                appointment.setConsult(consult);
                appointmentList1.add(appointment);
            }
        }
        return appointmentList1;

    }
}
